package com.zhuss.intention.processor;

import java.util.Date;

import com.zhuss.exception.ExceptionCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.intention.intention.DeletePersonalIntentionBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.intention.db.dao.ext.PersonalIntentionDao;
import com.zhuss.intention.db.entity.PersonalIntention;
import com.zhuss.util.JsonUtil;

/**
 * @description: 个人需求删除
 * @author: zhushanshuo
 * @create: Fri Dec 25 09:39:45 CST 2020
 **/
@Component("personalIntentionDelete")
public class PersonalIntentionDelete extends AbstractProcessor<Object, DeletePersonalIntentionBo> implements Processor<Object, DeletePersonalIntentionBo> {

    @Autowired
    private PersonalIntentionDao personalIntentionDao;

    @Autowired
	@Qualifier("personalIntentionRead")
	private PersonalIntentionRead personalIntentionRead;


    /**
     * 校验
     */
	@Override
    public void check(DeletePersonalIntentionBo bo) throws DzcxException {
		if (bo.getIntentionIdList().size() == 0) {
			throw new DzcxException(ExceptionCode.请选择数据);
		}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(DeletePersonalIntentionBo param) throws DzcxException {
    	//扩展，批量删除
		if (param.getIntentionIdList() != null) {
			param.getIntentionIdList().forEach(intentionId -> {
				PersonalIntention record = new PersonalIntention();
				record.setIntentionId(intentionId);
				record.setDeleted(param.getDeleted());

				record.setModifierId(param.getConcurrentUserId());
				record.setModifier(param.getConcurrentUserName());
				record.setModifyTime(new Date());
				personalIntentionDao.delete(record);
				try {
					log(param);
				} catch (DzcxException e) {
					e.printStackTrace();
				}

			});
		}
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(DeletePersonalIntentionBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.需求.key());
    	record.setAction(LogActionEnum.个人需求删除.key());
    	record.setTargetId(param.getIntentionId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
